package com.example.echoewish.ui.my_activity.friend_request_activity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.example.echoewish.R;
import com.example.echoewish.econstant.Constant;
import com.example.echoewish.pojo.MyPreConcernRequest;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class FriendRequestAdapter extends ArrayAdapter<MyPreConcernRequest> {
    Context context;
    int resource;

    private ButtonClickListener buttonClickListener;

    public FriendRequestAdapter(@NonNull Context context, int resource, @NonNull List<MyPreConcernRequest> objects) {
        super(context, resource, objects);
        this.context=context;
        this.resource=resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view=LayoutInflater.from(context).inflate(resource,parent,false);
        CircleImageView imageView=view.findViewById(R.id.friend_request_image_view);
        TextView textView=view.findViewById(R.id.friend_request_text_view);
        Button acceptButton=view.findViewById(R.id.friend_request_accept_button);
        Button refuseButton=view.findViewById(R.id.friend_request_refuse_button);

        MyPreConcernRequest myPreConcernRequest=getItem(position);
        if(myPreConcernRequest!=null)
        {
            textView.setText(myPreConcernRequest.name);
            if(myPreConcernRequest.headImage!=null&&!myPreConcernRequest.headImage.equals(""))
                Glide.with(context).load(Constant.HEAD_IMAGE_URL+myPreConcernRequest.headImage).into(imageView);
            else
                Glide.with(context).load(R.drawable.no_load).into(imageView);
        }

        acceptButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonClickListener.acceptButtonClickListener(position);
            }
        });

        refuseButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                buttonClickListener.refuseButtonClickListener(position);
            }
        });
        return view;
    }

    public void setButtonClickListener(ButtonClickListener buttonClickListener) {
        this.buttonClickListener = buttonClickListener;
    }

    public interface ButtonClickListener
    {
        void acceptButtonClickListener(int nowPosition);

        void refuseButtonClickListener(int nowPosition);
    }

}
